package com.woniu.utils;

/**
 * 封装响应结果实体对象
 * @param <T> 数据类型
 */
public class ResultObj<T> {  // 添加泛型参数<T>

    private Integer code;
    private String msg;
    public boolean success;
    private T data;  // 使用泛型T替代Object

    /**
     * 设置响应数据
     */
    public ResultObj<T> data(T result){  // 返回类型改为ResultObj<T>
        this.data = result;
        return this;
    }

    /**
     * 设置响应码
     */
    public ResultObj<T> code(Integer code){  // 返回类型改为ResultObj<T>
        this.code = code;
        return this;
    }

    public ResultObj<T> msg(String msg){  // 返回类型改为ResultObj<T>
        this.msg = msg;
        return this;
    }

    public ResultObj<T> success(boolean success){  // 返回类型改为ResultObj<T>
        this.success = success;
        return this;
    }

    /**
     * 定义静态方法封装请求成功信息
     */
    public static <T> ResultObj<T> ok() {  // 添加泛型方法支持
        ResultObj<T> result = new ResultObj<>();
        result.code = 200;
        result.msg = "请求成功";
        result.success = true;
        return result;
    }

    public static <T> ResultObj<T> ok(T data) {  // 添加泛型方法支持
        ResultObj<T> result = new ResultObj<>();
        result.code = 200;
        result.msg = "请求成功";
        result.success = true;
        result.data = data;
        return result;
    }

    /**
     * 封装请求失败的方法
     */
    public static <T> ResultObj<T> error() {  // 添加泛型方法支持
        ResultObj<T> result = new ResultObj<>();
        result.code = 500;
        result.msg = "请求失败";
        result.success = false;
        return result;
    }

    // 添加带错误信息的error方法
    public static <T> ResultObj<T> error(int code, String msg) {
        ResultObj<T> result = new ResultObj<>();
        result.code = code;
        result.msg = msg;
        result.success = false;
        return result;
    }

    // 保持原有的getter/setter方法
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {  // 返回类型改为T
        return data;
    }

    public void setData(T data) {  // 参数类型改为T
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
}
